package com.link.base.base.contract.service;

import com.link.base.base.contract.dao.mybatis.mapper.PriContItemMapper;
import com.link.base.base.contract.model.ModContract;
import com.link.base.base.contract.model.PriContItem;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.ServiceException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 价格合同产品信息
 *
 * @author 阳晓琳
 * @date 2020-10-14 16:52:38
 */
@Service
public class PriContItemServiceImpl extends BasicServiceImpl<PriContItem> implements PriContItemService {

    public static BigDecimal ZERO = new BigDecimal(0);

    @Resource
    private PriContItemMapper    priContItemMapper;

    @Resource
    private ModContractService modContractService;

    @Override
    public BasicMapper<PriContItem> getBasicMapper() {
        return priContItemMapper;
    }


    /**
     * 新建或编辑产品行，插入零件对应上年度价格合单价，并计算定点年降
     * @param entity
     * @throws Exception
     */
    @Override
    public void beforUpsert(PriContItem entity) throws Exception {
        //前端保存时提前预设了contractId
        ModContract modContr = new ModContract();
        modContr.setId(entity.getContractId());
        ModContract modContract = modContractService.queryById(modContr);
        if (modContract == null) {
            //无法关联当前价格合同，请联系管理员！
            throw new ServiceException("IYUCONTRACT-007");
        }
        //1.判断价格合同头是否首次签订 是，则上年度价格合同=null，
        if ("Y".equals(modContract.getFstPriFlag())) {
            entity.setLastYearNoTaxPrice(null);
        } else {
            //2.否，则根据头的上年度价格合同id,本行物料记录ID查询该合同对应零件，
            PriContItem querLastContItem = new PriContItem();
            if (modContract.getLastPriId() == null) {
                //上年度价格合同关联错误，请联系管理员！
                throw new ServiceException("IYUCONTRACT-008");
            }
            querLastContItem.setContractId(modContract.getLastPriId());
            querLastContItem.setMaterialCodeId(entity.getMaterialCodeId());
            PriContItem lastPriContItem = priContItemMapper.queryByMaterialCodeId(querLastContItem);
            if (lastPriContItem == null) {
                //说明没有查到当前零件在上一年度价格合同零件记录
                entity.setLastYearNoTaxPrice(null);
            } else {
                //将查到的上年度价格合同对应零件的未税单价赋值给新合同对应零件上年度价格合同
                entity.setLastYearNoTaxPrice(lastPriContItem.getNoTaxPrice());
                //实际年降=（上年度价格合同单价-未税单价）/上年度价格合同单价
                BigDecimal lastYearNoTaxPrice = entity.getLastYearNoTaxPrice() == null ? ZERO : entity.getLastYearNoTaxPrice();
                BigDecimal noTaxPrice = entity.getNoTaxPrice() == null ? ZERO : entity.getNoTaxPrice();
                //处理除数为零
                if (noTaxPrice.compareTo(ZERO) == 0) {
                    //未税单价为null,请正确录入未税单价！
                    throw new ServiceException("IYUCONTRACT-009");
                } else {
                    entity.setActYearDecline(lastYearNoTaxPrice.subtract(noTaxPrice).divide(lastYearNoTaxPrice,7,BigDecimal.ROUND_HALF_UP));
                }
            }
        }
    }
}